package ArrayList;

import java.util.*;

import static java.lang.System.in;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-08-25
 * Time: 18:49
 */
public class Test {
    public static void main(String[] args) {
        CardDemo cardDemo = new CardDemo();
        /**
         * 简单的洗牌算法:
         * 1. 买52张牌
         * 2. 洗牌（打乱）
         * 3. 3个人 每个人 轮流揭牌5张
         */

        //1.
        List<Card> cardList = cardDemo.buyCard();
        System.out.println(cardList);
        //2.
        cardDemo.shuffle(cardList);
        System.out.println(cardList);
        //3.
        List<List<Card>> ret = cardDemo.play(cardList);
        for (int i = 0; i < ret.size(); i++) {
            System.out.println("第"+ (i + 1) + "个人的牌："+ret.get(i));
        }

        System.out.println("剩下的牌：");
        System.out.println(cardList);
    }
    public static List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<>();
        //开头
        List<Integer> list0 = new ArrayList<>();
        list0.add(1);
        ret.add(list0);
        //从第二行开始处理
        for (int i = 1; i < numRows; i++) {
            //每一行的第一个和最后一个元素都是1
            List<Integer> curRow = new ArrayList<>();
            //一行中的第一个
            curRow.add(1);
            //一行的中间部分
            for (int j = 1; j < i; j++) {
                List<Integer> preRow = ret.get(i-1);
                int val1 = preRow.get(j);
                int val2 = preRow.get(j-1);
                curRow.add(val1+val2);
            }
            //一行中的最后一个
            curRow.add(1);
            ret.add(curRow);
        }
        return ret;
    }
    public static void main10(String[] args) {
        List<List<Integer>> ret = generate(4);
        for (int i = 0; i < ret.size(); i++) {
            for (int j = 0; j < ret.get(i).size(); j++) {
                System.out.print(ret.get(i).get(j) + " ");
            }
            System.out.println();
        }
    }
    public static List<List<Integer>> generate1(int numRows) {
        List<List<Integer>> ret = new ArrayList<>();
        List<Integer> list0 = new ArrayList<>();
        //开头
        list0.add(1);
        ret.add(list0);
        //从第二行开始处理
        for (int i = 1; i < numRows; i++) {
            List<Integer> curRow = new ArrayList<>();
            //处理第一个元素
            curRow.add(1);
            List<Integer> preRow = ret.get(i - 1);
            //中间
            for (int j = 1; j < i; j++) {
                int val1 = preRow.get(j);
                int val2 = preRow.get(j-1);
                curRow.add(val1 + val2);
            }

            //结尾
            curRow.add(1);
            ret.add(curRow);
        }

        return ret;
    }
    public static void main9(String[] args) {
        //杨辉三角问题
        List<List<Integer>> ret = generate1(4);
        for (int i = 0; i < ret.size(); i++) {
            for (int j = 0; j < ret.get(i).size(); j++) {
                System.out.print(ret.get(i).get(j) + " ");
            }
            System.out.println();
        }
    }
    public static void main8(String[] args) {
        List<List<Integer>> list = new ArrayList<>();//相当于二维数组用法
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list.add(list1);
        List<Integer> list2 = new ArrayList<>();
        list2.add(11);
        list2.add(12);
        list.add(list2);

        System.out.println(list);//[[1, 2], [11, 12]]
    }
    public static void main7(String[] args) {
        String str1 = "welcome to bit";
        String str2 = "come";
        ArrayList<Character> list = new ArrayList<>();

        for (int i = 0; i < str1.length(); i++) {
            char ch = str1.charAt(i);
            if (!str2.contains(ch+"")) {
                list.add(ch);
            }
        }
        for (Character x : list) {
            System.out.print(x);
        }
    }
    public static void main6(String[] args) {
        //boolean contains(Object o) 判断 o 是否在线性表中
        String str1 = "welcome to bit";
        String str2 = "come";
        //创建一个字符类型的集合list
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < str1.length(); i++) {
            char ch = str1.charAt(i);
            if(!str2.contains(ch+"")) {
                list.add(ch);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i));
        }
    }
    public static void main5(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        System.out.println(list);
        //输出方式：
        System.out.println("====使用迭代器iterator输出====");
        Iterator<Integer> it = list.iterator();
        while(it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();

        System.out.println("====使用迭代器listIterator输出====");
        ListIterator<Integer> it1 = list.listIterator();
        while(it1.hasNext()) {
            System.out.print(it1.next() + " ");
        }
        System.out.println();

        System.out.println("====使用迭代器listIterator输出 拓展====");
        ListIterator<Integer> it2 = list.listIterator(list.size());
        while(it2.hasPrevious()) {
            System.out.print(it2.previous() + " ");
        }
        System.out.println();

        System.out.println("====for循环输出====");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();

        System.out.println("====foreach 循环输出====");
        for (Integer x : list) {
            System.out.print(x + " ");
        }
        System.out.println();
    }
    public static void main4(String[] args) {
        //List<E> subList(int fromIndex, int toIndex) 截取部分 list（左闭右开）
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        System.out.println(list);
        System.out.println("==========");
        List<Integer> list1 = list.subList(2, 4);
        System.out.println(list1);
        System.out.println("==========");
        list1.set(0, 100);
        System.out.println(list);
        System.out.println(list1);
        /**
         * [1, 2, 100, 4, 5]
         * [100, 4]
         * 原因是list1压根没有创建新的对象，只是引用
         * 所以修改list1，也会把list中的值给修改
         */
    }
    public static void main3(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        //添加数据：
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println(list);
        System.out.println("============");

        List<Integer> test = new ArrayList<>();
        test.add(12);
        test.add(13);
        test.add(14);
        test.add(15);
        System.out.println(test);
        System.out.println("============");
        test.add(2, 10);
        System.out.println(test);
        //addAll: 尾插 c 中的元素
        test.addAll(list);
        System.out.println(test);//[12, 13, 10, 14, 15, 1, 2, 3, 4]
        System.out.println("============");
        //remove: 删除遇到的第一个 o
        test.remove(new Integer(10));
        System.out.println(test);
        test.remove(0);
        System.out.println(test);
    }
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        System.out.println(func(str));
    }
    private static String func(String str) {
        boolean[] flg = new boolean[127];
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(flg[ch] == false) {
                stringBuilder.append(ch);
                flg[ch] = true;
            }
        }
        return stringBuilder.toString();
    }
    public static void main1(String[] args) {
        int[] nums1 = {4, 5, 6};
        int[] nums2 = {1, 2, 3};
        merge(nums1, 3, nums2, 3);
    }
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = m-1;
        int j = n-1;
        int k = m + n -1;

        while(i >= 0 && j >= 0) {
            if(nums1[i] > nums2[j]) {
                nums1[k--] = nums1[i--];
            }else {
                nums1[k--] = nums2[j--];
            }
        }

        // while(i >= 0 ) {
        //     nums1[k--] = nums1[i--];
        // }

        while( j >= 0 ) {
            nums1[k--] = nums2[j--];
        }

    }
    public static void merge1(int[] nums1, int m, int[] nums2, int n) {
        int i = m - 1;
        int j = n - 1;
        int k = m + n - 1;

        /*while (i >= 0 && j >= 0) {
            if (nums1[i] > nums2[j]) {
                nums1[k--] = nums1[i--];
            } else {
                nums1[k--] = nums2[j--];
            }
        }*/
        while(i >= 0 && j >= 0) {
            if(nums1[i] > nums2[j]) {
                nums1[k--] = nums1[i--];
            }else {
                nums1[k--] = nums2[j--];
            }
        }

        while(i >= 0) {
            nums1[k--] = nums1[i--];
        }
        while(j >= 0) {
            nums1[k--] = nums2[j--];
        }

        System.out.println(nums1);
    }

}
